package qingke.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import qingke.mybatis.pojo.Product;
import qingke.mybatis.pojo.Store;
import qingke.util.GetEncoder;
import service.ProductService;
import service.StoreService;

@Controller
@RequestMapping
public class IndexController {

	@Autowired
	private StoreService storeService;

	@Autowired
	private ProductService productService;

	@RequestMapping("")
	public ModelAndView getAllStore(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("redirect:/index.jsp");
		List<Store> boutiques = storeService.getBoutiques();
		for (Store store : boutiques) {
			store.setStoreDescription(this.storeService.getDescriptionById(store.getId()));
		}
		List<Product> recommendedProducts = productService.getRecommendedProducts();
		request.getSession().setAttribute("boutiques", boutiques);
		request.getSession().setAttribute("recommendedProducts", recommendedProducts);
		request.getSession().setAttribute("choose", "1");
		return mv;
	}

	@RequestMapping("/select")
	public ModelAndView select(@RequestParam String selectname, HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		selectname = GetEncoder.getUtf8(selectname);
		Integer choose = Integer.valueOf(request.getParameter("choose"));
		ModelAndView mv = new ModelAndView();
		if (choose == 0) {
			mv.setViewName("redirect:/Store/getAllStoreBySelect?selectname=" + selectname);
			request.getSession().setAttribute("choose", "0");
		} else {
			mv.setViewName("redirect:/Product/getAllProductBySelect?selectname=" + selectname);
			request.getSession().setAttribute("choose", "1");
		}
		return mv;
	}

	@RequestMapping("/selectSessionAjax")
	public ModelAndView selectSessionAjax(String select, int choose, HttpServletRequest request)
			throws IOException, ServletException {
		ModelAndView mv = new ModelAndView();
		if (select != null) {
			request.getSession().setAttribute("select", select);
			if (choose == 0) {
				mv.setViewName("redirect:/Store/getAllStoreBySelect?selectname=" + select);
				request.getSession().setAttribute("choose", 0);
			} else {
				mv.setViewName("redirect:/Product/getAllProduct?selectname=" + select);
				request.getSession().setAttribute("choose", 1);
			}

		} else {
			request.getSession().removeAttribute("select");
			if (choose == 0) {
				mv.setViewName("redirect:/Store/getAllStoreBySelect");
				request.getSession().setAttribute("choose", 0);
			} else {
				mv.setViewName("redirect:/Product/getAllProduct");
				request.getSession().setAttribute("choose", 1);
			}
		}
		return mv;
	}
}
